/*
 * Copyright (C) 2009 The Antilapka Project Team ( antilapka@googlegroups.com )
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cz.dnk.android.gug.antilapka;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AntiLapka extends Activity implements OnClickListener{

	SensorManager mSensorManager;
	final int MENU_START_SERVICE = 1;
	final int MENU_STOP_SERVICE = 2;
	final int MENU_PREFERENCES = 3;
	final int MENU_CREDITS = 4;
	final int MENU_QUIT = 5;
	
	//private final static String[] batteryExtraKeys = {"level", "scale", "voltage", "temperature", "plugged", "status", "health", "present", "technology", "icon-small"};

	
	 /**
	  * {@inheritDoc}
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(0, MENU_START_SERVICE, 0, R.string.start_service).setIcon(android.R.drawable.star_on);
	    menu.add(0, MENU_STOP_SERVICE, 0, R.string.stop_service).setIcon(android.R.drawable.star_off);
	    menu.add(0, MENU_PREFERENCES, 0, R.string.menuitem_settings).setIcon(android.R.drawable.ic_menu_preferences);
	    menu.add(0, MENU_CREDITS, 0, R.string.menuitem_credits).setIcon(android.R.drawable.ic_menu_info_details);
	    menu.add(0, MENU_QUIT, 0, R.string.menuitem_quit).setIcon(android.R.drawable.ic_lock_power_off);
	    return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case MENU_START_SERVICE:
	    	startService(new Intent(AntiLapka.this, AntiLapkaService.class));
	        return false;
	    case MENU_STOP_SERVICE:
	    	stopService(new Intent(this.getApplicationContext(), AntiLapkaService.class));
	        return false;
	    case MENU_PREFERENCES:
	    	 startActivity(new Intent(this.getApplicationContext(), EditPreferences.class));
	         return false;
	    case MENU_CREDITS:
	        startActivity(new Intent(this.getApplicationContext(), AboutDialog.class));
	        return true;
	    case MENU_QUIT:
	    	stopService(new Intent(this.getApplicationContext(), AntiLapkaService.class));
	        finish();
	        return true;
	    }
	    return false;
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Set content listeners */
        
        Button button = (Button)findViewById(R.id.start_button);
        button.setOnClickListener(this);
        
        Button button2 = (Button)findViewById(R.id.stop_button);
        button2.setOnClickListener(this);
        
        //You can not receive this through components declared in manifests,
        //only by explicitly registering for it with Context.registerReceiver(). 
      /*  IntentFilter batteryStatusChanged = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        BroadcastReceiver receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
            	//http://android.git.kernel.org/?p=platform/packages/apps/Settings.git;a=blob;f=src/com/android/settings/BatteryInfo.java;h=ef60fc35b1f0f80f9bb88cc9d4fc5eaf55e9ef4b;hb=de2d9f5f109265873196f1615e1f3546b114aaa7
               	int status = intent.getIntExtra("status", 0);
               	int plugType = intent.getIntExtra("plugged", 0);
               	
               	///TODO: Plugged and full results in what?
               	if (status == BatteryManager.BATTERY_STATUS_CHARGING || plugType > 0) {
               		startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.Plugged", true));
 
               	} else {
               		startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.UnPlugged", true));
               	}

              // show first message 
              //Toast toast = Toast.makeText(context,"SMS Message: " + status, Toast.LENGTH_LONG);
              //toast.show();
             
            }
        }; 
        registerReceiver(receiver, batteryStatusChanged, null, null);*/
        
    }
    
    /** We are implementing OnClickListener in our activity object */
    public void onClick(View v){   
    	Log.e("onClick", "event fired");
    	if (v.equals(findViewById(R.id.start_button))) {
    		//startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.Plugged", true));
    		startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.Activate", true));
    		Log.e("start_button", "clicked");
    	}
    	if (v.equals(findViewById(R.id.stop_button))) {
    		//startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.UnPlugged", true));
    		startService(new Intent(AntiLapka.this, AntiLapkaService.class).putExtra("cz.dnk.gug.android.antilapka.Activate", false));
    		Log.e("stop_button", "clicked");
    		//SystemClock.sleep(5000);
    	}	   		
    }

}